linux shell 脚本判断某个文件是否有某个字符串以及统计含有某个字符串的个数然后做if else大于小于等于逻辑判断

您所在的位置:网站首页 shell 判断字符串是否包含 linux shell 脚本判断某个文件是否有某个字符串以及统计含有某个字符串的个数然后做if else大于小于等于逻辑判断

linux shell 脚本判断某个文件是否有某个字符串以及统计含有某个字符串的个数然后做if else大于小于等于逻辑判断

2024-07-13 09:45| 来源: 网络整理| 查看: 265

简介:本脚本主要用来学习判断某个文件是否有某个字符串以及统计含有某个字符串的个数,然后做if else大于小于等于逻辑判断去执行对应的操作。

一、测试脚本search_string.sh:

#!/bin/bash echo 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印"yes messycode",反之打印“no messycode” grep -w "messycode" compile_error_hint.c && echo "yes messycode"||echo "no messycode" echo -e "\n" echo 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,如果有打印"yes tanghanyue",反之打印“no tanghanyue” grep -w "tanghanyue" compile_error_hint.c && echo "yes tanghanyue"||echo "no tanghanyue" echo -e "\n" echo 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数 awk -v RS='\0' -F'tanghanyue' '{print NF-1}' compile_error_hint.c echo -e "\n" echo 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数 grep -rn "tanghanyue" compile_error_hint.c | wc -l echo -e "\n" echo 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,并把结果保存到number.txt文件里面 grep -rn "tanghanyue" compile_error_hint.c | wc -l >number.txt echo -e "\n" echo 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印"yes messycode",反之打印“no messycode” if grep -q "tanghanyue" compile_error_hint.c then echo "yes tanghanyue" else echo "no tanghanyue" fi echo -e "\n" echo 搜索compile_error_hint.c 文件里面是否包含“weifanghai”字符串,如果有打印"yes weifanghai",反之打印“no weifanghai” grep -q "weifanghai" compile_error_hint.c if [ $? -eq 0 ];then echo "yes weifanghai" else echo "no weifanghai" fi echo -e "\n" echo 搜索compile_error_hint.c里面包含"tanghanyue"字符串的个数,并打印出来。 number=$(grep -rn "tanghanyue" compile_error_hint.c | wc -l) echo $number echo -e "\n" a=10 if [ $a -eq 10 ] then echo "a = 10" fi echo -e "\n" echo "如果compile_error_hint.c里面包含tanghanyue字符串的个数大于2两个,打印“the number of tanghanyue is greater than 2 ”。" if [ $number -gt 2 ] then echo the number of tanghanyue is greater than 2 fi echo -e "\n" echo "如果compile_error_hint.c里面包含tanghanyue字符串的个数大于10两个,打印“second the number of tanghanyue is greater than 10 ”。" if (("$number" > "10" )) then echo second the number of tanghanyue is greater than 10 fi echo -e "\n" 二、脚本搜索的文件compile_error_hint.c:

weifanghai weifanghai weifanghai weifanghai weifanghai love love love love love love tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue 三、运行脚本,测试 结果:

weifanghai@weifanghai-VirtualBox:/work/shell$ ./search_string.sh 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印yes messycode,反之打印“no messycode” no messycode 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,如果有打印yes tanghanyue,反之打印“no tanghanyue” tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue tanghanyue yes tanghanyue 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数 9 显示compile_error_hint.c 文件里面包含“messycode”字符串的个数 9 搜索compile_error_hint.c 文件里面是否包含“tanghanyue”字符串,并把结果保存到number.txt文件里面 搜索compile_error_hint.c 文件里面是否包含“messycode”字符串,如果有打印yes messycode,反之打印“no messycode” yes tanghanyue 搜索compile_error_hint.c 文件里面是否包含“weifanghai”字符串,如果有打印yes weifanghai,反之打印“no weifanghai” yes weifanghai 搜索compile_error_hint.c里面包含tanghanyue字符串的个数,并打印出来。 9 a = 10 如果compile_error_hint.c里面包含tanghanyue字符串的个数大于2两个,打印“the number of tanghanyue is greater than 2 ”。 the number of tanghanyue is greater than 2 如果compile_error_hint.c里面包含tanghanyue字符串的个数大于10两个,打印“second the number of tanghanyue is greater than 10 ”。 weifanghai@weifanghai-VirtualBox:/work/shell$

贴个实际运行图:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3